/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ncsu.csc573.project1.util;

import edu.ncsu.csc573.project1.util.Constants.PPMethod;
import java.util.Scanner;

/**
 *
 * @author svpendse1
 */
public class PPRequest {
    private PPMethod exchangeMethod;
    private int rfcNumber;
    private String version;
    private HeaderField hostField;
    private HeaderField osField;
    
    public static class PPRequestBuilder {
        Constants.PPMethod exchangeMethod;
        int rfcNumber;
        String version;
        HeaderField hostField;
        HeaderField osField;
        
        public PPRequestBuilder() {
            
        }

        public PPRequestBuilder exchangeMethod(PPMethod method) {
            this.exchangeMethod = method;
            return this;
        } 
        
        public PPRequestBuilder rfcNumber(int rfcNumber) { 
            this.rfcNumber = rfcNumber;
            return this;
        }

        public PPRequestBuilder version(String version) {
            this.version = version;
            return this;
        }
        
        public PPRequestBuilder host(HeaderField field) { 
            this.hostField = field;
            return this;
        }

        public PPRequestBuilder os(HeaderField field) { 
            this.osField = field;
            return this;
        }

        public PPRequest build() {
            return new PPRequest(this);
        }
    }
    
    public PPRequest(PPRequestBuilder builder) {
        this.exchangeMethod = builder.exchangeMethod;
        this.version = builder.version;
        this.rfcNumber = builder.rfcNumber;
        this.hostField = builder.hostField;
        this.osField = builder.osField;
    }

    public PPMethod getExchangeMethod() {
        return exchangeMethod;
    }

    public void setExchangeMethod(PPMethod exchangeMethod) {
        this.exchangeMethod = exchangeMethod;
    }

    public HeaderField getHostField() {
        return hostField;
    }

    public void setHostField(HeaderField hostField) {
        this.hostField = hostField;
    }

    public HeaderField getOsField() {
        return osField;
    }

    public void setOsField(HeaderField osField) {
        this.osField = osField;
    }

    public int getRfcNumber() {
        return rfcNumber;
    }

    public void setRfcNumber(int rfcNumber) {
        this.rfcNumber = rfcNumber;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    
    
    //Return the string in the form of a string message consistent with the 
    //project description.
    public String getRequestMessage() {
        StringBuilder output = new StringBuilder();

        //Line 1
        output.append(exchangeMethod.getName());
        output.append(" RFC ");
        output.append(rfcNumber);
        output.append(" ");
        output.append(version);
        output.append("\r\n");

        //Line 2
        output.append("Host: ");
        output.append(hostField.getValue());
        output.append("\r\n");

        //Line 3
        output.append("OS: ");
        output.append(osField.getValue());
        output.append("\r\n");

        //Line 4
        output.append("\r\n");
        
        return output.toString();
    }

    //Parse an object of type PPRequest from an input string
    public static PPRequest parseFromString(String input) {

        PPRequest output = null;
        try {
            Scanner scanner = new Scanner(input);

            String method = scanner.next();
            PPMethod exchangeMethod = PPMethod.constructMethodFromName(method);
            if (exchangeMethod == null) {
                throw new Exception("Invalid exchange method " + method);
            }
            scanner.skip(" RFC ");
            int rfcNumber = scanner.nextInt();
            String version = scanner.next();
            scanner.skip("\r\n");
            scanner.skip("Host: ");
            String hostname = scanner.next();
            scanner.skip("\r\n");
            scanner.skip("OS: ");
            String osName = scanner.nextLine();
            scanner.skip("\r\n");

            output = new PPRequest.PPRequestBuilder().exchangeMethod(exchangeMethod).version(version).rfcNumber(rfcNumber).host(new HeaderField("Host", hostname)).os(new HeaderField("OS", osName)).build();
            return output;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        PPRequest request = new PPRequest.PPRequestBuilder().exchangeMethod(PPMethod.GET).host(new HeaderField("Host", "value")).os(new HeaderField("OS", "value OS Lion")).rfcNumber(1000).version("version").build();

        System.out.println(request.getRequestMessage().length()); 
        PPRequest.parseFromString(request.getRequestMessage());
        System.out.println(PPRequest.parseFromString(request.getRequestMessage()).getRequestMessage().length());
    }
}
